// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Eggman Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 68 : TYPE_EGGMAN

// Function declarations
reserve function Eggman_Function151

// Static Values

// Tables
//0x153A0
private table Eggman_table0
	7, 8, 8, 7, 7, 16
end table

function Eggman_Function151
	object.ypos = object.value1
	Sin256(temp0, object.value2)
	temp0 <<= 10
	object.ypos += temp0
	object.value2 += 2
	object.value2 &= 255
	object[+1].xpos = object.xpos
	object[+1].ypos = object.ypos
	object[+1].direction = object.direction
end function


event ObjectMain
	switch object.state
	case 0
		temp0 = object.ixpos
		temp0 -= screen.xcenter
		stage.newXBoundary1 = temp0
		temp0 = object.ixpos
		temp0 += screen.xcenter
		stage.newXBoundary2 = temp0
		if object[0].xpos > object.xpos
			object.animation = 1
			ResetObjectEntity(26, TypeName[Music Event], 0, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			object[+1].type = TypeName[Eggman Hammer]
			object[+1].value3 = 168
			object.ypos -= 0xF80000
			object.value1 = object.ypos
			object[+1].xpos = object.xpos
			object[+1].ypos = object.ypos
			object[+1].value0 = object.xpos
			object[+1].value1 = object.ypos
			object[+1].frame = 3
			object[+1].priority = PRIORITY_ACTIVE
			object[+5].type = TypeName[Eggman Totem]
			object[+5].xpos = object.ixpos
			object[+5].xpos -= screen.xcenter
			object[+5].xpos += 16
			object[+5].xpos <<= 16
			object[+1].value4 = object[+5].xpos
			object[+1].value4 += 0x5A0000
			object[+5].value3 = 136
			object[+5].drawOrder = 2
			object[+5].priority = PRIORITY_ACTIVE
			object[+1].value7 = 0xC800
			object[+1].value7 *= screen.xcenter
			object[+1].value7 /= 160
			object[+1].value12 = 0x30000
			object[+1].value12 *= screen.xcenter
			object[+1].value12 /= 160
			object[+6].type = TypeName[Eggman Totem]
			object[+6].xpos = object.ixpos
			object[+6].xpos += screen.xcenter
			object[+6].xpos -= 16
			object[+6].xpos <<= 16
			object[+1].value5 = object[+6].xpos
			object[+1].value5 -= 0x5A0000
			object[+6].value3 = 136
			object[+6].direction = FLIP_X
			object[+6].drawOrder = 2
			object[+6].priority = PRIORITY_ACTIVE
			object.value7 = 8
			object.priority = PRIORITY_ACTIVE
			object.state++
		end if
		break
	case 1
		break
	case 2
		object.value4++
		if object.value4 == 60
			if object.direction == FLIP_NONE
				object[+1].xvel = -0x10000
			else
				object[+1].xvel = 0x10000
			end if
			object[+1].yvel = -0x40000
			object[+1].state++
		end if
		if object.value4 == 180
			object.value4 = 0
			object.animation = 5
			object.value9 = 0
			object.state++
			ResetObjectEntity(26, TypeName[Music Event], 1, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
			object[+1].value8 = 1
		end if
		break
	case 3
		object.ypos += object.yvel
		object.yvel += 0x1800
		object.value4++
		if object.value4 == 38
			object.yvel = 0
			object.value4 = 0
			object.animationTimer = 0
			object[+1].value8 = 0
			object.state++
		end if
		object[+1].xpos = object.xpos
		object[+1].ypos = object.ypos
		object[+1].direction = object.direction
		break
	case 4
		if object.value4 < 48
			object.ypos += object.yvel
			object.yvel -= 0x800
			object.value4++
		else
			object.value4 = 0
			object.yvel = 0
			object.value1 = object.ypos
			object.state++
		end if
		object[+1].xpos = object.xpos
		object[+1].ypos = object.ypos
		object[+1].direction = object.direction
		break
	case 5
		object.ypos = object.value1
		CallFunction(Eggman_Function151)
		if object.value4 < 8
			object.value4++
		else
			object.value4 = 0
			object.animation = 6
			object.animationTimer = 0
			object.value5 = 2
			object.value6 = 0
			object.direction = FLIP_X
			object.priority = PRIORITY_ACTIVE
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
			object.state++
		end if
		break
	case 6
		object.xpos += 0x40000
		object.value1 -= 0x4000
		CallFunction(Eggman_Function151)
		if object.outOfBounds == 1
			object.type = TypeName[Blank Object]
			object[+1].type = TypeName[Blank Object]
		end if
		break
	end switch
	if object.value7 != 0
		if object.value8 > 0
			object.value8--
			GetBit(temp0, object.value8, 0)
			if temp0 == 1
				SetPaletteEntry(0, 192, 0xE0E0E0)
			else
				SetPaletteEntry(0, 192, 0)
			end if
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.value8 == 0
				CheckEqual(object[currentPlayer].animation, ANI_HURT)
				temp0 = checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DYING)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DROWNING)
				temp0 |= checkResult
				if temp0 != 0
					if object.animation != 2
						object.animation = 2
						object.animationTimer = 0
					end if
				end if
				BoxCollisionTest(C_TOUCH, object.entityPos, -24, -24, 24, 24, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
				if checkResult == 1
					CallFunction(PlayerObject_CheckHit)
					if checkResult == 1
						object.value7--
						if object.value7 == 0
							player.score += 1000
							object.animation = 4
							object.animationTimer = 0
							object.value5 = 0
							object.value9 = 1
							object.value1 = object.ypos
							object.value2 = object[+1].value2
							object.state++
							object[+1].state = 4
							object[+2].state = 2
							object[+3].state = 2
							object[+4].state = 2
							object[+5].value3 = 144
							object[+5].state = 3
							object[+6].value3 = 144
							object[+6].state = 3
						else
							object.animation = 3
							object.animationTimer = 0
							object.value8 = 64
							PlaySfx(SfxName[Boss Hit], 0)
						end if
					end if
				end if
			end if
		next
	end if
	if object.state != 6
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = object[currentPlayer].collisionRight
			temp0 <<= 16
			temp0 += object[currentPlayer].xpos
			temp1 = stage.curXBoundary2
			temp1 <<= 16
			if temp0 > temp1
				object[currentPlayer].xvel = 0
				object[currentPlayer].speed = 0
				object[currentPlayer].xpos = temp1
				temp0 = object[currentPlayer].collisionRight
				temp0 <<= 16
				object[currentPlayer].xpos -= temp0
			end if
		next
	end if
	if object.value9 == 1
		temp0 = oscillation
		temp0 &= 7
		if temp0 == 0
			Rand(temp0, 48)
			temp0 -= 24
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			object[tempObjectPos].drawOrder = 5
			PlaySfx(SfxName[Explosion], 0)
		end if
	end if
end event


event ObjectDraw
	switch object.animation
	case 1
		temp0 = object.animationTimer
		temp0 >>= 3
		if saveRAM[495] == 0
			object.animationTimer++
			object.animationTimer %= 15
		endif
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.animationTimer
		temp0 &= 15
		temp0 >>= 3
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			if object[0].animation != ANI_DYING
				object.animation = 1
			end if
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 3
		temp0 = object.animationTimer
		temp0 &= 1
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			object.animation = 1
		end if
		DrawSpriteFX(4, FX_FLIP, object.xpos, object.ypos)
		break
	case 4
		temp0 = object.animationTimer
		temp0 >>= 5
		temp0 += 4
		temp0 %= 5
		object.animationTimer++
		object.animationTimer &= 63
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 5
		DrawSpriteFX(5, FX_FLIP, object.xpos, object.ypos)
		break
	case 6
		temp0 = object.animationTimer
		temp0 >>= 3
		object.animationTimer++
		object.animationTimer &= 15
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		temp0 = object.animationTimer
		temp0 >>= 2
		temp0 &= 1
		if temp0 == 1
			DrawSpriteFX(15, FX_FLIP, object.xpos, object.ypos)
		end if
		break
	end switch
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone03")
	if checkResult == 1
		if saveRAM[495] == 0
			LoadSpriteSheet("Global/EggMobile.gif")
		else
			LoadSpriteSheet("Global/EggRoboMobile.gif")
		endif
		SpriteFrame(-28, -28, 60, 20, 5, 1)
		SpriteFrame(-28, -28, 60, 20, 66, 1)
		SpriteFrame(-28, -28, 60, 20, 5, 22)
		SpriteFrame(-28, -28, 60, 20, 66, 22)
		SpriteFrame(-28, -28, 60, 20, 5, 43)
		SpriteFrame(-28, -28, 60, 20, 66, 43)
		SpriteFrame(-32, -8, 64, 29, 1, 64)
		SpriteFrame(31, 4, 12, 8, 66, 65)
		SpriteFrame(30, 2, 14, 12, 66, 74)
		SpriteFrame(31, -2, 22, 14, 66, 87)
		SpriteFrame(31, -9, 32, 26, 94, 64)
		SpriteFrame(31, -2, 22, 14, 66, 87)
		SpriteFrame(31, -9, 32, 26, 94, 64)
		SpriteFrame(31, 4, 12, 8, 66, 65)
		SpriteFrame(30, 2, 14, 12, 66, 74)
		SpriteFrame(-3, -28, 11, 8, 79, 65)
		SpriteFrame(0, 0, 0, 0, 7, 2)
		if options.vsMode == 1
			foreach (TypeName[Eggman], arrayPos0, ALL_ENTITIES)
				object[arrayPos0].type = TypeName[Blank Object]
			next
		end if
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-28, -28, 60, 20, 1, 1)
		SpriteFrame(-28, -28, 60, 20, 62, 1)
		SpriteFrame(-28, -28, 60, 20, 1, 22)
		SpriteFrame(-28, -28, 60, 20, 62, 22)
		SpriteFrame(-28, -28, 60, 20, 1, 43)
		SpriteFrame(-28, -28, 60, 20, 62, 43)
		SpriteFrame(-32, -8, 64, 29, 415, 170)
		SpriteFrame(31, 4, 12, 8, 57, 90)
		SpriteFrame(30, 2, 14, 12, 57, 99)
		SpriteFrame(31, -2, 22, 14, 34, 97)
		SpriteFrame(31, -9, 32, 26, 72, 86)
		SpriteFrame(31, -2, 22, 14, 34, 97)
		SpriteFrame(31, -9, 32, 26, 72, 86)
		SpriteFrame(31, 4, 12, 8, 57, 90)
		SpriteFrame(30, 2, 14, 12, 57, 99)
		SpriteFrame(-3, -28, 11, 8, 45, 88)
		SpriteFrame(0, 0, 0, 0, 7, 2)
	end if
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
